﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;

namespace QLTB_CTK34
{
    public partial class FrmKetNoiCSDL : Form
    {
        public FrmKetNoiCSDL()
        {
            InitializeComponent();
            this.MaximizeBox = false;
        }

        private void FrmKetNoiCSDL_Load(object sender, EventArgs e)
        {
            cbbServerName.Text = Properties.Settings.Default["ServerName"].ToString();
            cbbDBName.Text = Properties.Settings.Default["DatabaseName"].ToString();
            txtUser.Text = Properties.Settings.Default["UserID"].ToString();
            txtPass.Text = Properties.Settings.Default["PassWord"].ToString();
        }
        private void cbbServerName_DropDown(object sender, EventArgs e)
        {
            cbbServerName.Cursor = Cursors.WaitCursor;
            cbbServerName.Items.Clear();
            SqlDataSourceEnumerator instance = SqlDataSourceEnumerator.Instance;
            System.Data.DataTable table = instance.GetDataSources();
            cbbServerName.Cursor = Cursors.Default;
            foreach (System.Data.DataRow row in table.Rows)
            {
                cbbServerName.Items.Add(row[table.Columns["ServerName"]] + "\\" + row[table.Columns["InstanceName"]]);
            }
           
        }
        private void cbbDBName_DropDown(object sender, EventArgs e)
        {
            
            cbbDBName.Cursor = Cursors.WaitCursor;
            using (var con = new SqlConnection(string.Format("Data Source={0};User Id= {1};Password= {2};",cbbServerName.Text,txtUser.Text,txtPass.Text)))
            {
                con.Open();
                DataTable databases = con.GetSchema("Databases");
                cbbDBName.Cursor = Cursors.Default;
                foreach (DataRow database in databases.Rows)
                {
                    cbbDBName.Items.Add(database.Field<String>("database_name"));
                }
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default["ServerName"] = cbbServerName.Text;
            Properties.Settings.Default["DatabaseName"] = cbbDBName.Text;
            Properties.Settings.Default["UserID"] = txtUser.Text;
            Properties.Settings.Default["PassWord"] = txtPass.Text;
            Properties.Settings.Default.Save();
            DialogResult = DialogResult.OK;
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

    }

}
